#include "ledControl.h"
#include "cmsis_os2.h"
#include "SEGGER_RTT.h"
#include "state.h"
#include "led.h"
osThreadId_t led_taskHandle;
const osThreadAttr_t ledTask_attributes = {
  .name = "led_task",
  .stack_size = 48 * 4,
  .priority = (osPriority_t) osPriorityNormal1,
};
osMessageQueueId_t ledQueue;
static void led_task(void *argument);
void ledControl_init(){
    ledQueue = osMessageQueueNew(1, sizeof(uint8_t), NULL);
    if(ledQueue==NULL) SEGGER_RTT_WriteString(0,"ledQueueError\r\n");
    led_taskHandle = osThreadNew(led_task, NULL, &ledTask_attributes);
    if(led_taskHandle==NULL)  SEGGER_RTT_WriteString(0,"led_taskHandleError\r\n");
}
static void led_task(void *argument){
  led_init();
  uint32_t delayTime=1000;
  for(;;){
    if(state == STATE_RUN){
      delayTime = 500;
      LED_TOGGLE(1);
    }
    else if(state == STATE_STOP){
      LED_ON(1);
      delayTime = 1000;
    }
    else if(state == STATE_CMD_OUTPUT){
      delayTime = 50;
      LED_TOGGLE(1);
    }
    else if(state == STATE_ERROR){
      LED_OFF(1);
      delayTime = 1000;
    }
    uint8_t tmp;
    osStatus_t state = osMessageQueueGet(ledQueue, &tmp, NULL, delayTime);
  }
}
void ledRouse(){
    uint8_t tmp = 0;
    osMessageQueuePut(ledQueue, &tmp, 0, 0);
}